package com.simon.mengkou.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.broadcast.BaseReceiver;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.ImageTag;
import com.simon.mengkou.data.enums.EImageTagType;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

/**
 * Created by xiazhenshui on 16/6/20.
 * 图片标签属性
 */

public class ImageTagPropertiesDialog extends Dialog  implements View.OnClickListener{
    //品牌
    private TextView mEtBrand;
    //价格
    private TextView mEtPrice;
    //地址
    private TextView mEtAddress;

    private float mRatioX;
    private float mRatioY;

    public ImageTagPropertiesDialog(Context context) {
        super(context, R.style.image_tag_theme_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image_tag_properties);
        setCanceledOnTouchOutside(false);

        mEtBrand = (TextView) findViewById(R.id.image_edit_tag_id_brand);
        mEtPrice = (TextView) findViewById(R.id.image_edit_tag_id_price);
        mEtAddress = (TextView) findViewById(R.id.image_edit_tag_id_address);

        findViewById(R.id.image_edit_tag_id_brand_btn).setOnClickListener(this);
        findViewById(R.id.image_edit_tag_id_price_btn).setOnClickListener(this);
        findViewById(R.id.image_edit_tag_id_address_btn).setOnClickListener(this);
        findViewById(R.id.image_edit_tag_id_finish).setOnClickListener(this);
        findViewById(R.id.image_edit_tag_id_cancel).setOnClickListener(this);


        final ActivityReceiver receiver = new ActivityReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(CstOuer.BROADCAST_ACTION.IAMGE_TAG_EDIT_ACTION);
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(receiver, filter);

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                LocalBroadcastManager.getInstance(getContext())
                        .unregisterReceiver(receiver);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_edit_tag_id_brand_btn:
                OuerDispatcher.presentImageTagSearchActivity(getContext(), EImageTagType.IMAGE_TAG_BRAND);
                break;
            case R.id.image_edit_tag_id_price_btn:
                OuerDispatcher.presentImageTagSearchActivity(getContext(), EImageTagType.IMAGE_TAG_PRICE);
                break;
            case R.id.image_edit_tag_id_address_btn:
                OuerDispatcher.presentImageTagSearchActivity(getContext(), EImageTagType.IMAGE_TAG_ADDRESS);
                break;
            case R.id.image_edit_tag_id_finish://完成
                goFinish();
                break;
            case R.id.image_edit_tag_id_cancel://取消
                goCancel();
                break;
        }
    }

    /**
     * 完成
     */
    private void goCancel() {
        cancel();
    }

    /**
     * 取消
     */
    private void goFinish() {
        String brand = mEtBrand.getText().toString().trim();
        String price = mEtPrice.getText().toString().trim();
        String address = mEtAddress.getText().toString().trim();

        if(UtilString.isBlank(brand)
                && UtilString.isBlank(price)
                && UtilString.isBlank(address)) {
            UtilOuer.toast(getContext(), R.string.image_tag_edit_properties_empty);
            return;
        }

        if(mListener != null) {
            ImageTag tag = new ImageTag();
            tag.setBrand(brand);
            tag.setPrice(price);
            tag.setLocation(address);
            tag.setXratio(mRatioX);
            tag.setYratio(mRatioY);
            mListener.onFinish(tag);
        }
        cancel();
    }


    public interface OnFinishListener {
        void onFinish(ImageTag tag);
    }

    private OnFinishListener mListener;

    public void setOnFinishListener(OnFinishListener listener) {
        mListener = listener;
    }

    public void setRatioXY(float ratioX, float ratioY) {
        mRatioX = ratioX;
        mRatioY = ratioY;
    }

    public void setImageTag(ImageTag tag) {
        mEtBrand.setText(tag.getBrand());
        mEtPrice.setText(tag.getPrice());
        mEtAddress.setText(tag.getLocation());
        setRatioXY(tag.getXratio(), tag.getYratio());
    }


    /**
     * 广播接收器
     * @author zhenshui.xia
     *
     */
    private class ActivityReceiver extends BaseReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent != null) {
                UtilLog.d(getClass().getSimpleName() + " action:" + intent.getAction());
                int type = intent.getIntExtra(CstOuer.KEY.TYPE, EImageTagType.IMAGE_TAG_BRAND);
                String value = intent.getStringExtra(CstOuer.KEY.TAGS);
                if(type == EImageTagType.IMAGE_TAG_BRAND) {
                    mEtBrand.setText(value);
                } else if(type == EImageTagType.IMAGE_TAG_PRICE) {
                    mEtPrice.setText(value);
                } else if(type == EImageTagType.IMAGE_TAG_ADDRESS) {
                    mEtAddress.setText(value);
                }
            }
        }
    }
}
